from PySide.QtCore import Qt
from Models import PlayGameModelProxy
import corefacade
import notifications

from puremvc.interfaces import ICommand
from puremvc.patterns.command import SimpleCommand
from View import VideoMediator, QMLMediator, ChatPanelMediator, BankPanelMediator, ActionPanelMediator, BalancePanelMediator
from PySide import QtDeclarative, QtCore

__author__ = 'CJay'


class StartupCommand(SimpleCommand,ICommand):
    def execute(self, notification):
        print "startup execute (command)"
        app = notification.getBody()

        self.facade.registerCommand(notifications.ACTION, ActionCommand)
        self.facade.registerCommand(notifications.CPU_TURN, CPUTurnCommand)
        self.facade.registerCommand(notifications.CPU_ACTION, CPUActionCommand)
        self.facade.registerCommand(notifications.NEWTURN, NewTurnCommand)
        self.facade.registerCommand(notifications.ENDTURN, EndTurnCommand)
        self.facade.registerCommand(notifications.FINISH, FinishCommand)


        self.facade.registerMediator(VideoMediator(app.video))
        self.facade.registerMediator(QMLMediator(app.qml))
        self.facade.registerMediator(ChatPanelMediator(app.qml.rootObject().findChild(QtCore.QObject,"chatPanel")))
        self.facade.registerMediator(BankPanelMediator(app.qml.rootObject().findChild(QtCore.QObject,"bankPanel")))
        self.facade.registerMediator(BalancePanelMediator(app.qml.rootObject().findChild(QtCore.QObject,"balance")))
        self.facade.registerMediator(ActionPanelMediator(app.qml.rootObject().findChild(QtCore.QObject,"actionspanel")))

        self.facade.registerProxy(PlayGameModelProxy())
        self.sendNotification(notifications.GAMESCREEN)



class GameScreenCommand(SimpleCommand,ICommand):
    '''
    launch when  game screen appears
    '''
    def execute(self, notification):
        #: @type: PlayGameModelProxy
        gameProxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)
        gameProxy.reset()

class EndTurnCommand(SimpleCommand, ICommand):
    def execute(self, notification):
        #: @type: PlayGameModelProxy
        gameProxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)
        action = notification.getBody().solve_action()
        self.sendNotification(notifications.NEWTURN,None)
        gameProxy.next()

class NewTurnCommand(SimpleCommand, ICommand):
    def execute(self, notification):
        gameProxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)
        gameProxy.setPCheck(False)
        gameProxy.setCPUCheck(False)
        if len(gameProxy.getRawPCards())==0:
            gameProxy.addPlayerCard()
            gameProxy.addPlayerCard()
            self.sendNotification(notifications.ADDCARD,"player")
        else:
            gameProxy.addCard()
            self.sendNotification(notifications.ADDCARD,"table")

class GameStartCommand(SimpleCommand,ICommand):
    '''
    This command launch when game starts/resets
    '''
    def execute(self, notification):
        app = notification.getBody()
#        self.facade.registerMediator(VideoMediator(app.video))
#        self.sendNotification(corefacade.AppFacade.PLAYVIDEO)

class ActionCommand(SimpleCommand, ICommand):
    def execute(self, notification):
        #: @type: PlayGameModelProxy
        gameproxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)
        action = notification.getBody()[0]
        gameproxy.setAction(action)
        if action=="blind":
            gameproxy.next()
            return
        if action=="dblind":
            gameproxy.next()
            return
        if action=="bet":
            gameproxy.next()
            return
        if action == "call":
            gameproxy.next()
            return
        if action == "check":
            gameproxy.next()
            return

        print "Player chooce UNRESOLVED variant (in ActionCommand): ",action

class CPUTurnCommand(SimpleCommand, ICommand):
    def execute(self, notification):
        gameproxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)
        cpu_action = gameproxy.getCurrentState().solve_action()
        self.sendNotification(notifications.CPU_ACTION,(cpu_action,str(gameproxy.getPPut())))

class CPUActionCommand(SimpleCommand,ICommand):
    def execute(self, notification):
        gameproxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)
        gameproxy.next()


class FinishCommand(SimpleCommand, ICommand):
    def execute(self, notification):
        print "Finish command executed"
        gameproxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)